#include <cmath>
#include "zhngdi.hpp"
#include "zhnmat.hpp"
#define MIN(a,b)                 ((a)<(b)?(a):(b))

double RGBtoGRAY(int rgb)
{
//    double ans = 0.299*(rgb & 0xFF0000 >> 16);
//    ans += 0.587*(rgb & 0x00FF00 >> 8);
//    ans += 0.114*(rgb & 0x0000FF);
    double ans = rgb & 0xFF0000 >> 16;
    ans += rgb & 0x00FF00 >> 8;
    ans += rgb & 0x0000FF;
    //return ((rgb & 0xFF0000) + (rgb & 0x00FF00) + (rgb & 0x0000FF))/3.0;
    return ans/3.0;
}
 
//SIFT方法获取图像的关键点和特征描述符
//param img: 单通道图像
//return: 关键点、特征描述符
void Get_Keypoints(const zhnmat::Mat& img)
{
    using namespace std;
    using namespace zhnmat;
    double sigma0 = 1.52;
    int featurepic = 3;  // 特征图数量
    double k = pow(2, (double)1 / featurepic);
    int stacknum = featurepic + 3;
    int octavenum = int(log2(MIN(img.row(), img.col()))) - 3;
    Mat sigma(octavenum, stacknum);
    for (int i=0; i<octavenum; ++i)
        for (int j=0; j<stacknum; ++j)
            sigma.set(i, j, sigma0 * pow(k, j) * (1<<i));
    vector<Mat> SamplePyramid{img};
    for (int i=1; i<octavenum; ++i)
        SamplePyramid.push_back(Mat(SamplePyramid[i-1], GENERATE_TYPE::DOWN_SAMPLE));
    cout << SamplePyramid[4].at(1, 23) << endl;  // 50

    // 获取高斯差分金字塔

 
    // 关键点定位
    return;
}

int main()
{
    using namespace zhngdi;
    using namespace zhnmat;
    Figure fig1("E:/qt/01.jpeg");
    POINT2D p(fig1.Get_WindowSize());
    Mat img1(p.y, p.x);
    for (int i=0; i<p.y; ++i)
        for (int j=0; j<p.x; ++j)
            img1.set(i, j, RGBtoGRAY(fig1.Get_PixelColor(j, i)));
    Get_Keypoints(img1);
    Figure::Window_Loop();
    return 0;
}
